package com.jt.tenant.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.jt.tenant.R;
import com.jt.tenant.app.JTApplication;
import com.jt.tenant.conn.GetNetData;
import com.jt.tenant.widget.Tools;
import com.zcx.helper.http.AsyCallBack;
import com.zcx.helper.util.UtilToast;

/**
 * Created by Administrator on 2016/2/14.
 */
public class DataRechargeActivity extends JTActivity implements View.OnClickListener {

    private TextView title_name;

    private TextView tv_datarecharge_now,tv_datarecharge_integral,tv_datarecharge_dk,tv_datarecharge_price,tv_datarecharge_pay;

    private EditText et_datarecharge_jf;

    private String tyjf;

    private String data_susplus;

    private double dk = 0;

    private double pay_price = 50.0;

    private double data_price = 50.0 ;

    private double min_num;

    private GetNetData getNetData= new GetNetData(new AsyCallBack<GetNetData.Info>(){

        @Override
        public void onSuccess(String toast, int type, GetNetData.Info info) throws Exception {

            super.onSuccess(toast, type, info);

                if(info.msg.equals("000")){

                    data_susplus = info.surplus;

                    tv_datarecharge_now.setText(Tools.F_num(data_susplus)+"M");

                }else {

                    UtilToast.show(context,info.msg);

                }


        }
    });

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_datarecharge);

        initView();

    }

    @Override
    protected void onResume() {

        super.onResume();

        tyjf = JTApplication.jtPreferences.readUserIntegral("2");

        min_num = data_price*10>Double.parseDouble(tyjf)?Double.parseDouble(tyjf):data_price*10;

        tv_datarecharge_integral.setText("通用积分："+tyjf);

    }

    private void initView() {

        (title_name = (TextView) findViewById(R.id.tv_title_name)).setText("流量充值");

        tv_datarecharge_now = (TextView)findViewById(R.id.tv_datarecharge_now);

        getNetData.execute(this);

        tv_datarecharge_integral = (TextView)findViewById(R.id.tv_datarecharge_integral);

        tv_datarecharge_dk = (TextView)findViewById(R.id.tv_datarecharge_dk);

        (tv_datarecharge_price = (TextView)findViewById(R.id.tv_datarecharge_price)).setText("实际支付：50元");

        (tv_datarecharge_pay = (TextView)findViewById(R.id.tv_datarecharge_pay)).setOnClickListener(this);

        et_datarecharge_jf = (EditText) findViewById(R.id.et_datarecharge_jf);

        et_datarecharge_jf.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {



            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

                if(!s.toString().equals("")) {

                    if (Integer.parseInt(Tools.F_num(s.toString())) > min_num) {

                        et_datarecharge_jf.setText(Tools.F_num(String.valueOf(min_num)));

                        dk = min_num / 10;

                    } else {

                        dk = Float.parseFloat(s.toString()) / 10;

                    }

                    tv_datarecharge_dk.setText("抵扣¥"+Tools.F_num(String.valueOf(dk))+"元");



                }else {

                    tv_datarecharge_dk.setText("抵扣¥0元");

                    dk = 0;

                }

                pay_price = data_price-dk;

                if(pay_price <= 0){

                    pay_price = 0.01;

                }

                tv_datarecharge_price.setText("实际支付："+Tools.F_num(String.valueOf(pay_price))+"元");

            }



            @Override
            public void afterTextChanged(Editable s) {

            }
        });


    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {

            case R.id.tv_datarecharge_pay:

                startActivity(new Intent(this,GSPaymentActivity.class)

                        .putExtra("order_id", "BCBLL"+System.currentTimeMillis())

                        .putExtra("total_price", Tools.F_num(String.valueOf(pay_price)))

//                                .putExtra("total_price", "0.01")

                        .putExtra("commonCount", Tools.F_num(String.valueOf(dk*10)))

                        .putExtra("flag", "5")

                );

                break;

        }

    }

}
